package com.meryoung.day2;

import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Assert;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import org.testng.reporters.Files;
import sun.reflect.misc.FieldUtil;


import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Field;

import static java.lang.Thread.*;

public class ActionsTest {
    /*
    我要点击百度首页的新闻链接
     */
    WebDriver driver;
    private Files FileUtils;



    @BeforeMethod
    public void openchromeTest01(){
        //设置chromedriver路径
        System.setProperty("webdriver.chrome.driver", "C:\\Users\\123\\IdeaProjects\\selenium1\\drivers\\chromedriver.exe");
        //实例化Chromedriver
        driver = new ChromeDriver();
    }
//    @Test
//    public void byxpath02(){
//        driver.get("https://www.baidu.com");
//        //css定位
//        WebElement keyField = driver.findElement(By.xpath("//*[@id=\\\"su\\\"]"));
//    }
    @Test
    public void chilckTest01() throws InterruptedException {
        driver.get("https://www.baidu.com");
        sleep(5000);
        //定位新闻
        WebElement newslink = driver.findElement(By.linkText("新闻"));
        sleep(5000);
        //点击新闻链接
        newslink.click();
        sleep(5000);
        String title1 = driver.getTitle();
        Assert.assertEquals(title1,"百度一下，你就知道");
        //获取当前页面的url
//        String url = driver.getCurrentUrl();
//        System.out.println(url);
//        //校验当前页面是不是新闻页面
//        Assert.assertEquals(url,"https://www.baidu.com/");
    }
    @Test
    public void sendkeysTest01() throws InterruptedException {
        driver.get("https://www.baidu.com");
        sleep(5000);
        //定位文本框
        WebElement keys = driver.findElement(By.id("kw"));
        sleep(5000);
        //输入文字
        keys.sendKeys("selenium");
        sleep(5000);
        //定位百度一下
        WebElement baidudianji = driver.findElement(By.id("su"));
        //点击
        baidudianji.click();
        sleep(5000);
        //校验title
        String title = driver.getTitle();
        Assert.assertEquals(title,"selenium_百度搜索");
    }
/*
打开百度首页
判断是否展示百度一下按钮
 */
    @Test
    public void isDisplayedTest(){
        driver.get("https://www.baidu.com");
        Boolean isDisplay = driver.findElement(By.id("su")).isDisplayed();
        Assert.assertTrue(isDisplay);
    }
    /*
    打开测试页面
    判断Volvo单选框被选中
     */
    @Test
    public void idSelectTest() throws InterruptedException {
        driver.get("C:\\Users\\123\\Desktop\\appium\\index.html");
        sleep(3000);
        WebElement element1 = driver.findElement(By.xpath("//*[@id=\"radio\"]/input[1]"));
        //点击
        element1.click();
        sleep(3000);
        Boolean b = element1.isSelected();
        Assert.assertTrue(b);

    }
    @Test
    public void isEnabledTest() throws InterruptedException {
        driver.get("C:\\Users\\123\\Desktop\\appium\\index.html");
        sleep(3000);
        Boolean b1 = driver.findElement(By.xpath("//*[@id=\"button\"]/input[1]")).isEnabled();
        Assert.assertFalse(b1);
    }
    /*
    截图百度首页

     */
//    @Test
//    public void shotTest(){
//        driver.get("https://www.baidu.com");
//        File file1 = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
//        try{
//            FileUtils.copyFile(file1,new File("D:\\TESET.PNG"));
//        }catch (IOException e) {
//            e.printStackTrace();
//        }
//    }
    @AfterMethod
    public void closedBrowser(){
        driver.quit();
    }
}
